﻿
using Dapper;
using HBase;
using HBase.db;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;

namespace hCommand 
{
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public  class One2ManyEngine  : GkTransaction
    {
        public BaseEntity m;
        public Type t;
        public List<ChildrenEntity> detList;
        public List<long> deleteIds;

        public One2ManyEngine() {
            db_lock = false; 
        }

        protected override String getTransKey() {
            return "One2ManyEngine";//出库入库同都变更货位需要同一把锁
        }
        protected override TransRes Execute()
        {
            
            if (m.id == 0) {
                insert(m);
            } else {
                update(m);
            }
            if (deleteIds != null && deleteIds.Count > 0) {
                
                var p = t.GetProperty("status");
                String tableName = ToStr.GetTableName(t);
                String sql = null;
                if (p == null) {
                    sql = "delete  from " + tableName + " where id in @deleteIds and pid=@pid";
                } else {
                    sql = "update   " + tableName + " set status=-2 where id in @deleteIds and pid=@pid";
                }
                exec(sql, new {deleteIds, pid = m.id });

            }
            if (detList != null) {
                foreach (var det in detList) {
                    if (det.id == 0) {

                        det.pid = m.id;
                        insert(det);
                    } else {
                        update(det);
                    }
                }
            }

            return new TransRes();
        }


      

    }
}
